package odgs.app.notuse;


import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractListModel;

import odgs.Contents;
import odgs.Directory;
import odgs.Explorer;
import odgs.ExplorerNode;
import odgs.listener.ExplorerListener;
import odgs.manager.ExecuteManager;

public class ExplorerListModel extends AbstractListModel implements ExplorerListener
{
	private Explorer explorer;
	private List list = new ArrayList();
	private ExecuteManager executeManager;

	public ExplorerListModel(Explorer explorer)
	{
		this.explorer = explorer;
		explorer.addExplorerModifyListener(this);
		list.addAll(explorer.getDirectorys());
		list.addAll(explorer.getContents());
	}

	public Object getElementAt(int arg0)
	{
		return list.get(arg0);
	}

	public int getSize()
	{
		return list.size();
	}

	public void select(Object selectedValue)
	{
		if (selectedValue instanceof Directory)
		{
			Directory dir = (Directory) selectedValue;
			explorer.setPosition(dir.getPath() + dir.getName() + "/");
		}
		else if(selectedValue instanceof Contents)
		{
			Contents con = (Contents) selectedValue;
			executeManager.execute(con);
		}
		else if(selectedValue instanceof String)
		{
			explorer.goParent();
		}
	}

	public void added(ExplorerNode parent, ExplorerNode node)
	{
		setedPosition("");
	}

	public void copied(ExplorerNode node)
	{
		setedPosition("");
	}
	
	public void modified(ExplorerNode node)
	{
		setedPosition("");
	}

	public void moved(ExplorerNode node)
	{
		setedPosition("");
	}

	public void removed(ExplorerNode parent, ExplorerNode node)
	{
		setedPosition("");
	}

	public void setedPosition(String position)
	{
		list.removeAll(list);
		if(!explorer.isRoot())
			list.add("..");
		
		if(explorer.getDirectorySize() != 0)
		{
			List<Directory> dirs = explorer.getDirectorys();
			for (Directory dir : dirs)
			{
				list.add(dir);
			}
		}
		
		if(explorer.getContentsSize() != 0)
		{
			List<Contents> cons = explorer.getContents();
			for (Contents con : cons)
			{
				list.add(con);
			}
		}
		
		fireContentsChanged(this, 0, list.size());
	}
}